﻿using ClassLibrary;
using ClassLibrary.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Forms
{
    public partial class GoodDetails : UIForm
    {
        ItemService itemService = new ItemService();
        GoodService goodService = new GoodService();
        User user;
        Good good;
        public delegate void FlashParentFormType();
        public event FlashParentFormType FlashParentForm;
        public GoodDetails(Good good)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.good = good;
            nameLbl.Text = good.GoodName;
            pictureBox.Image = ImageService.BytesToImage(itemService.GetItemByItemID(good.ItemID)[0].ItemPicture);
            this.user = goodService.GetBuyer(good);
            buyerLbl.Text = user.UserName;
            teleLbl.Text = user.UserTele;
            addressLbl.Text = user.UserAddress;
            if (good.GoodStatus)
            {
                CompleteBtn.Text = "已完成";
                CompleteBtn.Enabled = false;
            }
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }

        private void CompleteBtn_Click(object sender, EventArgs e)
        {
            if (ShowAskDialog("确认提示框"))
            {
                good.GoodStatus = true;
                goodService.ModifyGood(good);
                CompleteBtn.Text = "已完成";
                CompleteBtn.Enabled = false;
                FlashParentForm();
                ShowSuccessTip("商品确认完成");
                Close();
            }
            else
            {
                ShowErrorTip("商品取消确认");
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
